package com.lightningtrade.controller;

import com.lightningtrade.domain.SysUser;
import com.lightningtrade.log.MethodLog;
import com.lightningtrade.log.annotaion.Log;
import com.lightningtrade.log.enums.BusinessType;
import com.lightningtrade.service.SysLoginService;
import com.lightningtrade.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@Api(tags = "登录功能-0312杜晟玮")
@RestController
public class SysLoginController {

    @Autowired
    private SysLoginService loginService;

//    @Log(title = "用户注册", businessType = BusinessType.INSERT)
    @ApiOperation(value = "用户注册", notes = "用户注册功能")
    @PostMapping("/register")
    public AjaxResult register(@Validated @RequestBody SysUser user) {
        return loginService.register(user);
    }


//    @Log(title = "用户登录", businessType = BusinessType.OTHER)
    @ApiOperation(value = "用户登录", notes = "用户登录功能")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody SysUser user) {
        // 登录
        return loginService.login(user);
    }

//    @Log(title = "用户退出登录", businessType = BusinessType.OTHER)
    @ApiOperation(value = "用户退出登录", notes = "用户退出登出功能")
    @PostMapping ("/logout")
    public AjaxResult logout() {
        // 退出登录
        boolean result = loginService.logout();
        if (!result) {
            return AjaxResult.error("退出失败");
        }
        // 返回成功信息和跳转地址
//        String redirectUrl = "http://localhost:8080/swagger-ui/index.html#/登录功能-0312杜晟玮";
        return AjaxResult.success("退出成功",null);
    }

    @ApiOperation(value = "获取登录用户信息", notes = "获取登录用户信息功能")
    @GetMapping("/getLoginUser")
    public AjaxResult getLoginUser() {

        // 构建返回结果
        SysUser user =  loginService.getLoginUser();
        if (user == null) {
            return AjaxResult.error("获取失败");
        }
        // 返回用户名和手机号
        // 构建返回的用户信息对象
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("username", user.getUsername());
        userInfo.put("phone", user.getPhone());
        userInfo.put("uid", user.getUid());

        return AjaxResult.success("获取成功",userInfo);
    }
}
